//
//  XJ_MyViewController.m
//  XunJieProject
//
//  Created by xue on 2018/6/7.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_MyViewController.h"
#import "XJ_PeisonInfoViewController.h"
#import "XJ_ApplicateCashViewController.h"
#import "XJ_AccountRechargeViewController.h"
#import "XJ_RealNameAuthViewController.h"
#import "XJ_SetViewController.h"
#import "XJ_UserInfoModel.h"
#import "XJ_CardListModel.h"
#import "XJ_RealNameAuthView.h"
#import "BC_KeFuAlextView.h"
#import "XJ_NewCardViewController.h"
#import "XJ_QuxianViewController.h"
#import "AppDelegate.h"

@interface XJ_MyViewController ()
@property (weak, nonatomic) IBOutlet UIView *infoView;
@property (weak, nonatomic) IBOutlet UIImageView *logoImage;
@property (weak, nonatomic) IBOutlet UILabel *lineLabel;
@property (weak, nonatomic) IBOutlet UIButton *logoutBtn;
@property (weak, nonatomic) IBOutlet UILabel *balanceLabel;
@property (weak, nonatomic) IBOutlet UIButton *freezeBalanceBtn;
@property (weak, nonatomic) IBOutlet UILabel *mobileLabel;
@property (weak, nonatomic) IBOutlet UILabel *customerServicePhoneLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *up_scrollView;

@property (strong ,nonatomic) XJ_UserInfoModel *userInfoModel;

@end

@implementation XJ_MyViewController
-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    [self loadUserInfo];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
}
-(void)creatMainUI{
    [self hideMySelfNavgation];
    
    _infoView.backgroundColor =Color_ZhuSeDiao;
    _logoImage.layer.cornerRadius =CGRectGetWidth(_logoImage.frame)/2;
    _lineLabel.backgroundColor =RGBCOLOR(183, 85, 35);
    [_logoutBtn setTitleColor:Color_ZhuSeDiao forState:UIControlStateNormal];
    _up_scrollView.constant =kStatusBarHeight;
    
}
- (IBAction)Click_btn:(id)sender {
    UIButton *button =(UIButton*)sender;
    switch (button.tag) {
        case 0://基本资料
        {
            XJ_PeisonInfoViewController *info =[[XJ_PeisonInfoViewController alloc]init];
            info.userInfoModel =self.userInfoModel;
            [self.navigationController pushViewController:info animated:YES];
        }
            break;
        case 1://基本资料
        {
            XJ_PeisonInfoViewController *info =[[XJ_PeisonInfoViewController alloc]init];
            info.userInfoModel =self.userInfoModel;
            [self.navigationController pushViewController:info animated:YES];
        }
            break;
        case 2://实名认证
        {
            ////identifyFlag 是否实名认证 1：是 0：否
            NSInteger identifyFlag =[_userInfoModel.identifyFlag integerValue];
            if (identifyFlag==1) {
                XJ_RealNameAuthView *alext =[[XJ_RealNameAuthView alloc]initWithTitle:@"温馨提示" Message:@"已实名认证" sureBtn:nil cancleBtn:@"确定"];
                [alext showXLAlertView];
            }else{
                XJ_RealNameAuthViewController *info =[[XJ_RealNameAuthViewController alloc]init];
                [self.navigationController pushViewController:info animated:YES];
            }
            
        }
            break;
        case 3://提现申请
        {
            ////identifyFlag 是否实名认证 1：是 0：否
            NSInteger identifyFlag =[_userInfoModel.identifyFlag integerValue];
            if (identifyFlag==1) {
                XJ_ApplicateCashViewController *cash =[[XJ_ApplicateCashViewController alloc]init];
                cash.userInfoModel =self.userInfoModel;
                [self.navigationController pushViewController:cash animated:YES];
            }else{
                XJ_RealNameAuthViewController *info =[[XJ_RealNameAuthViewController alloc]init];
                info.backRefurbishData = ^{
                    
                    [self loadUserInfo];
                };
                
                [self.navigationController pushViewController:info animated:YES];
            }
            
        }
            break;
        case 4://充值
        {
            ////identifyFlag 是否实名认证 1：是 0：否
            NSInteger identifyFlag =[_userInfoModel.identifyFlag integerValue];
//            NSArray *cardListArr =_userInfoModel.cardList;
            if (identifyFlag==1) {
                
                XJ_QuxianViewController *quxian =[[XJ_QuxianViewController alloc]init];
                quxian.userInfoModel =self.userInfoModel;
                [self.navigationController pushViewController:quxian animated:YES];
                
//                if (cardListArr.count>0) {
//
//
//
//                    XJ_AccountRechargeViewController *recharge =[[XJ_AccountRechargeViewController alloc]init];
//                    recharge.cardListArray =cardListArr;
//                    recharge.userInfoModel =self.userInfoModel;
//                    [self.navigationController pushViewController:recharge animated:YES];
//                }else{
//                    XJ_NewCardViewController *newCard =[[XJ_NewCardViewController alloc]init];
//                    newCard.cardListArray =cardListArr;
//                    [self.navigationController pushViewController:newCard animated:YES];
//
//                }
            }else{
                XJ_RealNameAuthViewController *info =[[XJ_RealNameAuthViewController alloc]init];
                info.backRefurbishData = ^{
                    
                    [self loadUserInfo];
                };
                
                [self.navigationController pushViewController:info animated:YES];
            }
            
            
        }
            break;
        case 5://设置
        {
            XJ_SetViewController *set =[[XJ_SetViewController alloc]init];
            set.userInfoModel =self.userInfoModel;
            [self.navigationController pushViewController:set animated:YES];
        }
            break;
        case 6://电话
        {
            NSString *phoneNum=_userInfoModel.customerServicePhone;
            BC_KeFuAlextView *alext =[[BC_KeFuAlextView alloc]initWithMessage:@"电话号码" sureBtn:@"拨打" cancleBtn:@"关闭" phone:_userInfoModel.customerServicePhone delete:NO];
            alext.resultIndex = ^(NSInteger index) {
                
                NSMutableString *str=[[NSMutableString alloc]initWithFormat:@"tel:%@",phoneNum];
                
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:phoneNum.length>0?phoneNum:@"暂无电话" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"关闭" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    
                }];
                
                UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"拨打" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    
                    
                    if (phoneNum.length>0) {
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
                    }
                    
                    
                }];
                
                
                
                // Add the actions.
                
                [alertController addAction:cancelAction];
                [alertController addAction:otherAction];
                [self presentViewController:alertController animated:YES completion:nil];
            };
            [alext showXLAlertView];
        }
            break;
        case 7://退出登录
        {
            
            XJ_RealNameAuthView *alext =[[XJ_RealNameAuthView alloc]initWithTitle:@"提示" Message:@"确定退出吗？" sureBtn:@"是" cancleBtn:@"否"];
            alext.resultIndex = ^(NSInteger index) {
                
                if (index==2) {
                    [self loadUserLoginOut];
                }
                
            };
            [alext showXLAlertView];
            
        }
            break;
            
        default:
            break;
    }
}
#pragma mark--获取我的信息
-(void)loadUserInfo{
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:USERID forKey:@"userId"];
    [dic setValue:token forKey:@"token"];
    
    [XJ_Utils signPost:@"user/info?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        
        XJ_UserInfoModel * model = [XJ_UserInfoModel mj_objectWithKeyValues:dataDic];
        NSMutableArray *array =[[NSMutableArray alloc]init];
        NSArray *cardList =dataDic[@"cardList"];
        for (NSDictionary *dic in cardList) {
            XJ_CardListModel * model1 = [XJ_CardListModel mj_objectWithKeyValues:dic];
            [array addObject:model1];
        }
        model.cardList =array;
        self.userInfoModel =model;
        
        self.balanceLabel.text =[NSString stringWithFormat:@"￥%@",model.balance];
        [self.freezeBalanceBtn setTitle:[NSString stringWithFormat:@"￥%@",model.freezeBalance] forState:UIControlStateNormal];
        if (model.mobile.length>5) {
            self.mobileLabel.text =[model.mobile stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        }else{
            self.mobileLabel.text =model.mobile;
        }
        
        self.customerServicePhoneLabel.text =[NSString stringWithFormat:@"全国客服电话%@",model.customerServicePhone];
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}

#pragma mark--退出登录
-(void)loadUserLoginOut{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:USERID forKey:@"userId"];
    [dic setValue:token forKey:@"token"];
    
    [XJ_Utils signPost:@"/user/loginout?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
        [db setObject:@"" forKey:@"token"];
        [db synchronize];
        
        /**进入首页**/
        [(AppDelegate*)[UIApplication sharedApplication].delegate crearRootLoginController];
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
